<?php

namespace App\Admin\Controllers;

use DB;
use Validator;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use App\AdminPermission;
use App\AdminRole;

class RoleController extends Controller
{
	/**
	 * [index 角色列表]
	 * @return [type] [description]
	 */
	public function index(Request $request)
	{
		$roles = AdminRole::where('id', '!=', 0);
		if ($request->input('keywords')) {
			$keywords = $request->input('keywords');
			$roles = $roles->where(function($roles) use ($keywords) {
				$roles->where('name', 'like', '%' . $keywords . '%')->orWhere('description', 'like', '%' . $keywords . '%');
			});
		}
		$roles = $roles->paginate(10);
		$roles = $roles->appends([
				'keywords' => $request->input('keywords')
			]);
		return view('/admin/role/index', compact('roles'));
	}

	/**
	 * [create 创建角色]
	 * @return [type] [description]
	 */
	public function create()
	{
		return view('/admin/role/add');
	}

	/**
	 * [store 创建角色行为]
	 * @return [type] [description]
	 */
	public function store()
	{
		$this->validate(request(), [
				'name' => 'required|min:3',
				'description' => 'required'
			]);

        \App\AdminRole::create(request(['name', 'description']));

		return redirect('/admin/roles');
	}

	/**
	 * [permission 角色权限关系]
	 * @return [type] [description]
	 */
	public function permission(\App\AdminRole $role)
	{
		$permissions = \App\AdminPermission::all();
		$myPermissions = $role->permissions;
		$pers = AdminPermission::select(DB::raw('id,label'))->get()->toArray();
		$pers = array_pluck($pers, 'label', 'id');
		$pers[0] = '-';

		return view('/admin/role/permission', compact('permissions', 'myPermissions', 'role', 'pers'));
	}

	/**
	 * [storePermission 储存角色权限]
	 * @return [type] [description]
	 */
	public function storePermission(\App\AdminRole $role)
	{
		$this->validate(request(), [
				'permissions' => 'required|array'
			]);
		$permissions = \App\AdminPermission::findMany(request('permissions'));
		$myPermissions = $role->permissions;
		$addPermissions = $permissions->diff($myPermissions);
		foreach ($addPermissions as $permission) {
			$role->grantPermission($permission);
		}
		$deletePermissions = $myPermissions->diff($permissions);
		foreach ($deletePermissions as $permission) {
			$role->deletePermission($permission);
		}

		return redirect('/admin/roles');
	}

	/**
	 * [delete description]
	 * @param  AdminRole $role [description]
	 * @return [type]          [description]
	 */
	public function delete(AdminRole $role)
	{
		$flag = false;
		$msg = '';
		if ($role->users->isEmpty()) {
			$role->delete();
		} else {
			$msg = '此角色已分配给用户，无法删除!';
		}
		return [
			'flag' => $flag,
			'msg' => $msg,
			'role_id' => $role->id
		];
	}

	/**
	 * [batchDelete description]
	 * @return [type] [description]
	 */
	public function batchDelete(Request $request)
	{
		$ids = $request->input('data');
		if ($ids) {
			$msg = '';
			foreach ($ids as $id) {
				if (!AdminRole::find($id)->users->isEmpty()) {
					$msg .= '"'.AdminRole::find($id)->name . '", ';
				}
			}
			if ($msg) {
				return [
					'flag' => false,
					'msg' => '角色 ' . trim($msg, ',') . '已经分配给用户了，无法删除!'
				];
			} else {
				AdminRole::whereIn('id', $ids)->delete();

				return [
					'flag' => true,
					'msg' => '',
					'role_id' => $ids
				];
			}
		} else {
			return [
				'flag' => false,
				'msg' => '请选择角色!'
			];
		}
	}

}